#include "thread_pool.h"

extern MYSQL* conn;

void pwdCommand(task_t * task)
{
    int userid = 0;

    //得到userid
    int ret = getUserid(task->peerfd, &userid);
    if(ret){
        fprintf(stderr, "Get userid error\n");
        sendMsg(task->peerfd, MSG_TYPE_FAILURE, 
                "An error occurred while retrieving the user name.");
        return ;
    }
    printf("userid %d\n", userid);

    //查询得到pwd
    char query[256] = {0};
    sprintf(query,
            "SELECT pwd FROM t_user WHERE userid=%d", userid);
    ret = mysql_query(conn, query);
    MYSQL_RES * res = mysql_store_result(conn);
    MYSQL_ROW row = mysql_fetch_row(res);
    char pwd[256] = {0};
    strcpy(pwd, row[0]);
    mysql_free_result(res);
    printf("pwd: %s\n", pwd);

    sendMsg(task->peerfd, MSG_TYPE_SUCCESS, pwd);

}

void pwdCommand1(task_t * task)
{
    printf("execute pwd command.\n");
    char path[128] = {0};
    char* err = getcwd(path, sizeof(path));
    train_t t;
    memset(&t, 0, sizeof(t));
    if(err == NULL)
    {
        strcpy(t.buff, "pwd error");
        t.len = strlen(t.buff);
        sendn(task->peerfd, &t, 4 + t.len);
        syslog(LOG_INFO, 
               "User execute PWD command: FAILED.");
    }
    else {
        strcpy(t.buff, path);
        t.len = strlen(t.buff);
        sendn(task->peerfd, &t, 4 + t.len);
        syslog(LOG_WARNING, "User execute PWD command: SUCCESSFUL.");
    }
}
